﻿using System.Diagnostics;

namespace DimensionsHelper.LanguageService.Syntax;

// OutputDataSource(name[, "description"])
//    [ConnectionString = "<connection_string> | UseInputAsOutput = True|False"]
//    [MetaDataOutputName = "<metadata_location>"]
//    [UpdateQuery = "<update_query>"]
//    [TableOutputName = "<table_output_name>"]
//    [VariableOrder = "SELECTORDER"]
// End OutputDataSource

public class OutputDataSourceSection : DmsSectionWithDescription
{
    private DmsProperty? _connectionStringProperty;
    private DmsProperty? _metadataOutputNameProperty;
    private DmsProperty? _tableOutputNameProperty;
    private DmsProperty? _updateQueryProperty;
    private DmsProperty? _useInputAsOutputProperty;
    private DmsProperty? _variableOrderProperty;

    internal OutputDataSourceSection(SyntaxToken declareKeywordToken, Identifier name, StringLiteral? description,
        SyntaxNode? parent) :
        base(SyntaxKinds.OutputDataSourceSection, declareKeywordToken, name, description, parent)
    {
    }


    /// <summary>
    ///     [ConnectionString = "connection_string | UseInputAsOutput = True|False"]
    /// </summary>
    public DmsProperty? UseInputAsOutputProperty => _useInputAsOutputProperty;


    /// <summary>
    ///     [ConnectionString = "connection_string | UseInputAsOutput = True|False"]
    /// </summary>
    public DmsProperty? ConnectionStringProperty => _connectionStringProperty;


    /// <summary>
    ///     [MetaDataOutputName = "metadata_location"]
    /// </summary>
    public DmsProperty? MetadataOutputNameProperty => _metadataOutputNameProperty;


    /// <summary>
    ///     [UpdateQuery = "update_query"]
    /// </summary>
    public DmsProperty? UpdateQueryProperty => _updateQueryProperty;


    /// <summary>
    ///     [TableOutputName = "table_output_name"]
    /// </summary>
    public DmsProperty? TableOutputNameProperty => _tableOutputNameProperty;


    /// <summary>
    ///     [VariableOrder = "SELECTORDER"]
    /// </summary>
    public DmsProperty? VariableOrderProperty => _variableOrderProperty;


    public OutputDataSourceSection Update(
        DmsProperty? useInputAsOutput,
        DmsProperty? connectionString,
        DmsProperty? metadataOutputName,
        DmsProperty? updateQuery,
        DmsProperty? tableOutputName,
        DmsProperty? variableOrder)
    {
        SetChildNullableField(ref _useInputAsOutputProperty, useInputAsOutput);
        SetChildNullableField(ref _connectionStringProperty, connectionString);
        SetChildNullableField(ref _metadataOutputNameProperty, metadataOutputName);
        SetChildNullableField(ref _updateQueryProperty, updateQuery);
        SetChildNullableField(ref _tableOutputNameProperty, tableOutputName);
        SetChildNullableField(ref _variableOrderProperty, variableOrder);
        return this;
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        Accept(visitor, DeclareKeywordToken, NameIdentifier, Description,
            UseInputAsOutputProperty, ConnectionStringProperty, MetadataOutputNameProperty,
            UpdateQueryProperty, TableOutputNameProperty, VariableOrderProperty);
    }
}


public static partial class NodeFactory
{
    public static OutputDataSourceSection CreateOutputDataSourceSection(SyntaxNode? parent,
        SyntaxToken declareKeywordToken, Identifier identifierName, StringLiteral? description)
    {
        Debug.Assert(declareKeywordToken.IsNode(SyntaxKinds.OutputDataSourceKeywordToken));
        return new OutputDataSourceSection(declareKeywordToken, identifierName, description, parent);
    }
}